package com.zhengdongzulin.zuke.app.main.apartment

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.lxj.androidktx.core.*
import com.xuexiang.xui.widget.picker.widget.TimePickerView
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder
import com.xuexiang.xui.widget.picker.widget.configure.TimePickerType
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.HouseTypeDetaisl
import com.zhengdongzulin.zuke.app.data.RoomDetails
import com.zhengdongzulin.zuke.base.*
import com.zhengdongzulin.zuke.databinding.ZkActivityReserveYudingBinding
import com.zhengdongzulin.zuke.ext.*
import com.zhengdongzulin.zuke.utils.DateFormatConstants
import com.zhengdongzulin.zuke.utils.DateUtils
import java.text.SimpleDateFormat
import java.util.*

/**
 * @类名称: CLASS
 * @类描述: 预定
 * @创建人：Jeff
 * @创建时间：2021/6/9 20:59
 * @备注：
 */
class ZKReserveYuDingActivity : BaseActivity<ReserveVm, ZkActivityReserveYudingBinding>() {
    var mTimePicker: TimePickerView? = null
    lateinit var mHouseTypeData: HouseTypeDetaisl//
    lateinit var mRoomDetaileData: RoomDetails//
    override fun getLayoutResId(): Int = R.layout.zk_activity_reserve_yuding

    override fun initVM(): ReserveVm = getVM(ReserveVm::class.java)

    override fun initView() {
        val calendar: Calendar = Calendar.getInstance()
        calendar.setTime(DateUtils.millis2Date(System.currentTimeMillis()))
        mTimePicker = TimePickerBuilder(
            this
        ) { date, v ->
            mView.time.text =
                DateUtils.date2String(date, SimpleDateFormat(DateFormatConstants.yyyyMMdd))
        }
            .setTimeSelectChangeListener { }
            .setType(TimePickerType.DEFAULT)
            .setCancelColor(color(R.color.color_1273f7))
            .setSubmitColor(color(R.color.color_1273f7))
            .setTitleColor(color(R.color.color_1273f7))
            .setDividerColor(color(R.color.color_1273f7))
            .setTextColorCenter(color(R.color.color_1273f7))
            .setTitleText("时间选择")
            .setOutSideCancelable(false)
            .setDate(calendar)
            .build()
    }

    override fun initData() {

        mShareViewModel.userInfo.value?.let {
            mView.name.setText(it.nameCn)
            mView.phone.setText(it.phone)
        }
        if (intent.getStringExtra("type") == "houseDetails") {
            mHouseTypeData = mShareViewModel.houseDetails.value!!
            if (mHouseTypeData.picList != null && mHouseTypeData.picList.size > 0) {
                mView.ivTitle.loadImage(mHouseTypeData.picList[0].url)
            }
            mView.titleName.text = mHouseTypeData.roomTypeName
            mView.money.text = "￥${mHouseTypeData.houseMinMoney}"
            mView.money.appendSizeSpan("/月", 0.46f)
                .colorSpan(
                    "",
                    mView.money.text.length - 2..mView.money.text.length,
                    color(R.color.black_66)
                )
                .styleSpan("", 0..mView.money.text.length - 2)
            mView.desc.text =
                "${mHouseTypeData.doorName}\u0020\u0020${mHouseTypeData.area}㎡\u0020\u0020${mHouseTypeData.orientation}"
            mView.addressDesc.text = mHouseTypeData.address
            mViewModel.applyBookInfo(mHouseTypeData.id)
        } else {
            mRoomDetaileData = mShareViewModel.roomDetails.value!!
            if (mRoomDetaileData.allPicList != null && mRoomDetaileData.allPicList.size > 0) {
                mView.ivTitle.loadImage(mRoomDetaileData.allPicList[0].url)
            }
            mView.titleName.text = mRoomDetaileData.houseName
            mView.money.text = "￥${mRoomDetaileData.rentPrice}"
            mView.money.appendSizeSpan("/月", 0.46f)
                .colorSpan(
                    "",
                    mView.money.text.length - 2..mView.money.text.length,
                    color(R.color.black_66)
                )
                .styleSpan("", 0..mView.money.text.length - 2)
            mView.desc.text =
                "${mRoomDetaileData.doorName}\u0020\u0020${mRoomDetaileData.area}㎡\u0020\u0020${mRoomDetaileData.orientation}"
            mView.addressDesc.text = mRoomDetaileData.address
            mViewModel.applyBookInfo(mRoomDetaileData.houseId)
        }
    }

    @Suppress("DEPRECATION")
    override fun initListener() {
        mView.selTime.setOnClickListener {
            hideKeyboard()
            mTimePicker?.show()
        }
        mView.call.setOnClickListener {
            if (mView.name.isEmpty()) {
                toast("请输入姓名")
                return@setOnClickListener
            }
            if (!mView.phone.checkPhone()) {
                toast("请输入正确的手机号")
                return@setOnClickListener
            }
            if (mView.time.isEmpty()) {
                toast("请选择时间")
                return@setOnClickListener
            }

            showXieYiDialog()
        }
    }

    var lastDay = "7"
    var earnestMoney = "1000"

    override fun startObserve() {
        mViewModel.reservation_apply.observe(this) {
            getDialog("预定成功", "您已预定成功") {
                mShareViewModel.houseDetails.value = null
                mShareViewModel.roomDetails.value = null
                finish()
            }.show()
        }
        mViewModel.reservation_apply.state.observe(this, getObserver(mViewModel.reservation_apply))

        mViewModel.apply_book_info_res.observe(this, androidx.lifecycle.Observer {
            lastDay = it.lastDay
            earnestMoney = it.earnestMoney
            mView.tvEmpty11.setText("温馨提示：请在${lastDay}天内完成签约")
            mView.tvEmpty12.setText("定金：${earnestMoney}元")
        })
        mViewModel.apply_book_info_res.state.observe(
            this,
            getObserver(mViewModel.apply_book_info_res)
        )
    }

    fun showXieYiDialog() {
        var isSelect = true
        var v = View.inflate(this, R.layout.popwindow_house_introduce_yu_ding, null)
        var dialog = getBottomDialog(v, 0.4)
        v.findViewById<View>(R.id.iv_close).setOnClickListener { dialog.dismiss() }
        var tvPay = v.findViewById<TextView>(R.id.tv_pay)
        v.findViewById<TextView>(R.id.project_desc).text =
            "尊敬的租客朋友，请您在缴纳定金${lastDay}天内与我们签订合同并完成付款，届时定金将冲抵本次合同相应金额。如逾期，定金将不予退还，敬请理解！      再次感谢您选择郑东租赁，尽享生活新主意！"
        v.findViewById<ImageView>(R.id.iv_radio).setOnClickListener {
            isSelect = !isSelect
            v.findViewById<ImageView>(R.id.iv_radio)
                .setImageResource(if (isSelect) R.mipmap.radio_select else R.drawable.radio_no_select)

            tvPay.setBackgroundResource(if (isSelect) R.color.color_1273f7 else R.color.shadow_color)
            tvPay.isClickable = isSelect
        }
        tvPay.setOnClickListener {
            dialog.dismiss()
            mViewModel.book_pay_now(
                houseShouDingId = "",
                houseId = mRoomDetaileData.houseId,
                type = "weChat",
                zukeName = mView.name.text.toString(),
                zukePhone = mView.phone.text.toString(),
                signDate = mView.time.text.toString(),
                remark = mView.message.text.toString(),
                money = earnestMoney
            )

//                mViewModel.reservation_apply.value = ""
//                if (intent.getStringExtra("type") == "houseDetails") {
//                    mViewModel.reservation_apply(
//                        name = name.getTextZ(),
//                        phone = phone.getTextZ(),
//                        seeTime = time.getTextZ(),
//                        note = message.getTextZ(),
//                        roomTypeId = mHouseTypeData.id
//                    )
//                } else {
//                    mViewModel.reservation_apply(
//                        name = name.getTextZ(),
//                        phone = phone.getTextZ(),
//                        seeTime = time.getTextZ(),
//                        note = message.getTextZ(),
//                        houseId = mRoomDetaileData.houseId
//                    )
//                }
        }
        dialog.show()
    }

}